#!/bin/bash

# JayVii
# https://notabug.org/jayvii
# infochecker v2.0

# counting files (ignoring folders)
MANY_TRACK=$(find $1 -type f | wc -l)
ALREADY_EXIST=0

# start checking
while (($MANY_TRACK > 0))
do

	# choosing next file
	CURRENT_TRACK=$(find $1 -type f | sed -n "${MANY_TRACK}","${MANY_TRACK}"p)

	# checking if file is a picture
	PICTURE=$(echo "$CURRENT_TRACK" | grep -o -i "jpeg\|jpg\|png\|bmp")
	while [ -n "$PICTURE" ]
	do
		MANY_TRACK=$((${MANY_TRACK} - 1))
		CURRENT_TRACK=$(find $1 -type f | sed -n "${MANY_TRACK}","${MANY_TRACK}"p)
		PICTURE=$(echo "$CURRENT_TRACK" | grep -o "jpg\|png\|bmp")
	done

	# assigning variables
	TITLE=$(mediainfo --Inform="General;%Title%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")
	ARTIST=$(mediainfo --Inform="General;%Album/Performer%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")
	TRACKARTIST=$(mediainfo --Inform="General;%Performer%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")
	ALBUM=$(mediainfo --Inform="General;%Album%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")
	YEAR_ORIG=$(mediainfo --Inform="General;%Original/Released_Date%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")
	YEAR=$(mediainfo --Inform="General;%Released_Date%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")
	YEAR_RECORD=$(mediainfo --Inform="General;%Recorded_Date%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")
	TRACKNUM=$(mediainfo --Inform="General;%Track/Position%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")
	FILESUFFIX=$(echo $CURRENT_TRACK | awk -F . '{print $NF}')
	#FILENAME=$($1/${CURRENT_TRACK%.*})

	# checking if variables are set
	if [ -z "$TITLE" ]
	then
		echo "Title:        $CURRENT_TRACK" >> missing_info.txt
	fi

	if [ -z "$TRACKARTIST" ]
	then
		echo "Track-Artis:  $CURRENT_TRACK" >> missing_info.txt
	fi

	if [ -z "$ARTIST" ]
	then
		echo "Album-Artist: $CURRENT_TRACK" >> missing_info.txt
	fi

	if [ -z "$ALBUM" ]
	then
		echo "Album:        $CURRENT_TRACK" >> missing_info.txt
	fi

	if [ -z "$YEAR" ] && [ -z "$YEAR_ORIG" ] && [ -z "$YEAR_RECORD" ]
	then
		echo "Year:        $CURRENT_TRACK" >> missing_info.txt
	fi

	if [ -z "$TRACKNUM" ]
	then
		echo "Track:       $CURRENT_TRACK" >> missing_info.txt
	fi

	# control output
	echo "################################"
	echo "[$MANY_TRACK]"
	echo "Current: $CURRENT_TRACK"
	echo " "

	MANY_TRACK=$((${MANY_TRACK} - 1))

done

echo " "
echo "################################"
echo "I am done!"
echo "See file 'missing_info.txt'."
